
import UIKit


class TF_AdListView: UIView {
    @objc public var array = [UIView]()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.isHidden = true
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        
        
    }
    

    
    @objc  public func add(ADLidt:[TFAdModel])
    {
        
        for vc in self.array {
            
            vc.removeFromSuperview()
        }
        self.array.removeAll()
     
        
        
        for ad in ADLidt {
            
            let imageView = TF_LivingImageViewTouches.init(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
            
            imageView.didTouchedBlock =  { [weak self](str) in
                guard self != nil else {
                    return
                }
                if (str.length>0) {
                    TalkfunSharedTools.openUrl(str: str)
                
                    
                }
                
            }
                                          
            imageView.backgroundColor = UIColor.clear
            imageView.sd_setImage(with: NSURL.init(string: ad.img) as URL?, completed: nil)
            
//            [imageView sd_setImageWithURL:[NSURL URLWithString:ad.img]];
            imageView.img = ad.link
            let imageViewWidth = self.bounds.size.width * (CGFloat(ad.scale) / 100.0);
            let proportion      = CGFloat(ad.width)/imageViewWidth;
            let imageViewHeight = CGFloat(ad.height) / proportion;
            let imageViewX = (self.frame.size.width - imageViewWidth) * (CGFloat(ad.position.left)/100.0)
            let imageViewY = (self.frame.size.height - imageViewHeight) * (CGFloat(ad.position.top)/100.0)
            imageView.frame = CGRect(x: imageViewX, y: imageViewY, width: imageViewWidth, height: imageViewHeight)
            
            ;
            imageView.contentMode = .scaleAspectFit
         
            self.addSubview(imageView)
            self.array.append(imageView)
           
        }
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
